#include "GameCheckBox.h"
#include "MyRender.h"
#include "MyInputMgr.h"
#include "ConsoleWnd.h"

extern CMyRender g_kRender;
extern CMyInputMgr g_kInputMgr;
extern CConsoleWnd g_kConsoleWnd;

CGameCheckBox::CGameCheckBox(void)
{
	m_eWndType = WT_CHECK_BOX;
	m_bChecked = false;
}

CGameCheckBox::~CGameCheckBox(void)
{
}

void CGameCheckBox::LoadResFromDir( const string& rkDir )
{
	m_kUnCheckedNormal.Load(rkDir + "/" + "unchecked_normal.bmp");
	m_kUnCheckedFocus.Load(rkDir + "/" + "unchecked_focus.bmp");
	m_kUnCheckedClick.Load(rkDir + "/" + "unchecked_click.bmp");
	m_kUnCheckedDisable.Load(rkDir + "/" + "unchecked_disable.bmp");

	m_kCheckedNormal.Load(rkDir + "/" + "checked_normal.bmp");
	m_kCheckedFocus.Load(rkDir + "/" + "checked_focus.bmp");
	m_kCheckedClick.Load(rkDir + "/" + "checked_click.bmp");
	m_kCheckedDisable.Load(rkDir + "/" + "checked_disable.bmp");
	return;
}

void CGameCheckBox::LoadResFromFile( const string& rkFile )
{
	CMyImage kImage;
	kImage.Load(rkFile);

	int iWidth = kImage.GetWidth() / 8;
	int iHeight = kImage.GetHeight();

	m_kUnCheckedNormal.LoadFromImage(kImage,0,0,iWidth,iHeight);
	m_kUnCheckedFocus.LoadFromImage(kImage,iWidth,0,iWidth,iHeight);
	m_kUnCheckedClick.LoadFromImage(kImage,iWidth * 2,0,iWidth,iHeight);
	m_kUnCheckedDisable.LoadFromImage(kImage,iWidth * 3,0,iWidth,iHeight);

	m_kCheckedNormal.LoadFromImage(kImage,iWidth * 4,0,iWidth,iHeight);
	m_kCheckedFocus.LoadFromImage(kImage,iWidth * 5,0,iWidth,iHeight);
	m_kCheckedClick.LoadFromImage(kImage,iWidth * 6,0,iWidth,iHeight);
	m_kCheckedDisable.LoadFromImage(kImage,iWidth * 7,0,iWidth,iHeight);

	return;
}

bool CGameCheckBox::OnLeftBtnDown( int iX,int iY )
{
	if (IsDisable())
	{
		return true;
	}

	m_spkLockControl = this;
	if (m_pkParent)
	{
		m_pkParent->ProcChildMsg(CM_LBtnDown,m_kName,m_kUserData);
	}

	return true;
}

bool CGameCheckBox::OnLeftBtnUp( int iX,int iY )
{	
	if (IsDisable())
	{
		return true;
	}

	if (m_pkParent)
	{
		m_pkParent->ProcChildMsg(CM_LBtnUp,m_kName,m_kUserData);
	}

	if (m_spkLockControl == this)
	{
		m_bChecked = !m_bChecked;
		ChildMsg eMsgType = m_bChecked ? CM_CheckBoxChecked : CM_CheckBoxUnChecked;
		if (m_pkParent)
		{
			m_pkParent->ProcChildMsg(eMsgType,m_kName,m_kUserData);

			g_kConsoleWnd.PrintFormatLog("m_bChecked = %d",m_bChecked);
		}
	}

	m_spkLockControl = NULL;
	return true;
}

void CGameCheckBox::OnDraw()
{
	CMyImage* pkImage = NULL;
	if (m_bChecked)
	{
		pkImage = &m_kCheckedNormal;	

		if (m_spkMouseOnControl == this)
		{
			pkImage = &m_kCheckedFocus;
		}

		if (m_spkMouseOnControl == this && g_kInputMgr.IsLeftBtnDown())
		{
			pkImage = &m_kCheckedClick;
		}

		if (IsDisable())
		{
			pkImage = &m_kCheckedDisable;
		}		
	}
	else
	{
		pkImage = &m_kUnCheckedNormal;	

		if (m_spkMouseOnControl == this)
		{
			pkImage = &m_kUnCheckedFocus;
		}

		if (m_spkMouseOnControl == this && g_kInputMgr.IsLeftBtnDown())
		{
			pkImage = &m_kUnCheckedClick;
		}

		if (IsDisable())
		{
			pkImage = &m_kUnCheckedDisable;
		}
	}

	if (pkImage)
	{
		g_kRender.DrawImage(*pkImage,m_iAbsX,m_iAbsY,m_iWidth,m_iHeight,(m_iAlpha << 24) | 0x00FFFFFF);
	}

	int iTextHeight = GetFontHeight(m_dwFontInfo);

	if (IsDisable())
	{
		g_kRender.DrawText(m_kText.c_str(),m_iAbsX + m_iWidth + 5,m_iAbsY + (m_iHeight - iTextHeight) / 2,0xFF8F8F8F);
	}
	else
	{
		g_kRender.DrawText(m_kText.c_str(),m_iAbsX + m_iWidth + 5,m_iAbsY + (m_iHeight - iTextHeight) / 2,m_dwTextColor);
	}	

	return;
}


void CGameCheckBox::SetChecked( bool bChecked )
{
	m_bChecked = bChecked;
}

bool CGameCheckBox::GetChecked()
{
	return m_bChecked;
}

void CGameCheckBox::OnSetText()
{
	return;
}
